var style = getStyleSheet('notes');
if (style != null) {
   addRule(style,'.note',
'position: relative; float: left; margin-right: 15px; margin-bottom: 15px;');
    
    addRule(style,'.notetitle, .notearea, .notearearo', 'clear: left;');
    addRule(style,'.notearea, .notearearo', 'width: 100%; height: 120px; margin-bottom: 25px;font-family: Verdana, Geneva, sans-serif;');
    
    addRule(style,'.notearearo', 'background-color: #eeeeee;');
    addRule(style,'.notetitle', 'width: 150px; overflow: hidden;');
    
    addRule(style,'.welcomenote', 'font-weight: bold;');
    
    addRule(style,'.width100', 'width: 100%; position: relative; float: left;');
    
    addRule(style,'.roarea', 'white-space: pre;overflow:auto;');
    
    addRule(style,'.noteb', 
'float: right;width: 70px;margin-left: 15px;text-align: center;font-size: 80%;cursor: pointer;background-color: #e0e0e0;border-width: 1px;border-color: #606060;border-style: solid;');
}



if (typeof(notes) == 'undefined') {

    var notes = [];
    
    var currentId;
    function setCurrentId(id){
        currentId = id;
    }
    
    function getCurrentId(){
        return currentId;
    }
    
    function selectNoteAction(){
        if (!isWait()) {
            var noteid = this.getAttributeNode("id");
            if (isNull(noteid)) {
                return false;
            }
            
            var ind = noteid.value.indexOf('_');
            if (ind == -1) {
                return false;
            }
            
            var id = noteid.value.substr(ind + 1, noteid.value.length - ind);
            setCurrentId(id);
            
            if (noteid.value.substr(0, 7) == "actent_") {
                selectEditNote();
            }
            else if (noteid.value.substr(0, 7) == "actant_") {
                selectApplyNote();
            }
            else if (noteid.value.substr(0, 7) == "actrnt_") {
                selectRemoveNote();
            }
            else if (noteid.value.substr(0, 7) == "actcnt_") {
                selectCancelNote();
            }
            setWait();
        }
    }
    
    /**
     * callback for cancel note button
     */
    function selectCancelNote(){
        var params = 'action=cancel&id=' + getCurrentId();
        sendRequest('note', params, editApplyNoteResponse);
    }
    
    
    /**
     * callback for select an edit note button
     */
    function selectEditNote(){
        var params = 'action=edit&id=' + getCurrentId();
        sendRequest('note', params, editApplyNoteResponse);
    }
    
    /**
     * callback for select an edit note button
     */
    function selectApplyNote(){
        var title = encodeURIComponent(getText(document.getElementById('ntt_' + getCurrentId())));
        var text = encodeURIComponent(getText(document.getElementById('ntv_' + getCurrentId())));
        var params = 'action=apply&id=' + getCurrentId() + '&title=' + title + '&note=' + text;
        sendRequest('note', params, editApplyNoteResponse);
    }
    
    /**
     * callback for remove an note
     */
    function selectRemoveNote(){
        var params = 'action=remove&id=' + getCurrentId();
        sendRequest('note', params, removeNoteResponse);
    }
    
    
    function editApplyNoteResponse(){
        if (httpRequest.readyState == 4) {
            if (httpRequest.status == '200') {
                var notes = document.getElementById('nts');
                if (isNull(notes)) {
                    return false;
                }
                
                var newNote = document._importNode(httpRequest.responseXML.documentElement, true);
                var oldNote = document.getElementById('mainnt_' + getCurrentId());
                
                if (isNull(oldNote)) {
                    return false;
                }
                
                notes.replaceChild(newNote, oldNote);
                
                bodynotes();
            }
            else {
                setDebugMessage('Response error: ' + httpRequest.status + ' ' + httpRequest.statusText);
            }
            cleanWait();
        }
    }
    
    function removeNoteResponse(){
        if (httpRequest.readyState == 4) {
            if (httpRequest.status == '200') {
                var oldNote = document.getElementById('mainnt_' + getCurrentId());
                if (isNull(oldNote)) {
                    return false;
                }
                oldNote.parentNode.removeChild(oldNote)
                
                bodynotes();
            }
            else {
                setDebugMessage('Response error: ' + httpRequest.status + ' ' + httpRequest.statusText);
            }
            cleanWait();
        }
    }
    
    
    function selectNewNote(){
        if (!isWait()) {
        
            var params = 'action=new';
            sendRequest('note', params, newNoteResponse);
            
            setWait();
        }
    }
    
    function newNoteResponse(){
        if (httpRequest.readyState == 4) {
            if (httpRequest.status == '200') {
                var notes = document.getElementById('nts');
                if (isNull(notes)) {
                    return false;
                }
                
                var importedNode = document._importNode(httpRequest.responseXML.documentElement, true);
                notes.appendChild(importedNode);
                
                bodynotes();
            }
            else {
                setDebugMessage('Response error: ' + httpRequest.status + ' ' + httpRequest.statusText);
            }
            cleanWait();
        }
    }
    
    function bodynotes(){
    
        // initialize menu's items
        var notes = document.getElementById("nts");
        if (notes != null) {
            var childs = notes.getElementsByTagName("div");
            if (isNull(childs)) {
                return false;
            }
            
            for (var i = 0; i < childs.length; i++) {
                var noteId = childs[i].getAttributeNode("id");
                if (isNull(noteId)) {
                    return false;
                }
                if (noteId.value.substr(0, 3) == "act") {
                    childs[i].onclick = selectNoteAction;
                }
            }
            document.getElementById("newnote").onclick = selectNewNote;
        }
        
        var setting = document.getElementById("actset");
        if (setting != null) {
            setting.onclick = saveSetting;
        }
    };
}


bodynotes();

